package com.woniuxy.controller;

import com.woniuxy.entity.User;
import com.woniuxy.entity.utils.ResponseEntity;
import com.woniuxy.jwt.JWTUtils;
import com.woniuxy.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * TODO
 *
 * @Description
 * @Author jresm骏
 * @Date 2022/12/5 19:56
 **/

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    AuthService authService;
    @GetMapping("/login")
    public ResponseEntity login(User user){
        User dbUser =  authService.findUser(user);
        //比较登录的是否密码和数据库密码是否一致，我们暂时不用密码加密
        if (Objects.nonNull(dbUser)&&dbUser.getPassword().equals(user.getPassword())){
            //说明登录成功
            String token = JWTUtils.generateToken(user.getUsername());
            //正常这里就需要存储到redis中！！！还要开发一个刷新token！
            ResponseEntity success = ResponseEntity.SUCCESS;
            success.put("token",token);
            return success;
        }else {
            return ResponseEntity.ERROR;
        }
    }
}
